<template>
  <div class="dashboard">
    <!-- 顶部统计卡片 -->
    <el-row :gutter="24" class="stats-header">
      <el-col :xs="24" :sm="6" v-for="(stat, idx) in stats" :key="idx">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-info">
              <p class="stat-title">{{ stat.title }}</p>
              <div class="stat-value-wrap">
                <span class="stat-value">{{ stat.value }}</span>
                <el-tooltip effect="light" content="查看详情">
                  <el-icon class="info-icon" size="16">
                    <InfoFilled />
                  </el-icon>
                </el-tooltip>
              </div>
              <p class="stat-change" :class="stat.trendClass">
                {{ stat.changeDesc }}
              </p>
            </div>
            <div class="stat-icon">
              <el-icon :size="28">
                <component :is="stat.icon" />
              </el-icon>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 核心内容区域 -->
    <el-row :gutter="24" class="main-content">
      <!-- 左侧用户分析 -->
      <el-col :span="8">
        <el-card class="user-analysis-card">
          <div class="chart-title">
            <h3>用户行为分析</h3>
            <el-button type="text" size="small">查看更多</el-button>
          </div>
          <div ref="barChart" class="bar-chart" style="height: 280px;"></div>
          <div class="user-overview">
            <h4>用户概述</h4>
            <p class="growth-info">比上周 <span :class="userGrowthClass">+23%</span></p>
            <p class="desc">我们为您创建了多个选项，可将它们组合在一起并定制为像素完美的页面</p>
            <div class="stats-grid">
              <div class="stat-item" v-for="(item, idx) in userStats" :key="idx">
                <span class="item-value">{{ item.value }}</span>
                <span class="item-label">{{ item.label }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 右侧访问量分析 -->
      <el-col :span="16">
        <el-card class="visit-analysis-card">
          <div class="chart-title">
            <h3>访问量趋势</h3>
            <span class="growth-tag" :class="visitGrowthClass">
              今年增长 {{ visitGrowth }}%
            </span>
          </div>
          <div ref="lineChart" class="line-chart" style="height: 320px;"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import * as echarts from 'echarts';
// Element Plus 图标 - 修复：替换为存在的图标
import { 
  PieChart, User, Mouse, DataLine,  // 使用DataLine代替不存在的TrendChart
  InfoFilled, ArrowUp, ArrowDown 
} from '@element-plus/icons-vue'; 

// 统计卡片数据 - 修复：更新图标引用
const stats = ref([
  {
    title: '总访问次数',
    value: '9120',
    icon: 'PieChart',
    changeDesc: '较上周 +20%',
    trendClass: 'up'
  },
  {
    title: '在线访客数',
    value: '182',
    icon: 'User',
    changeDesc: '较上周 +10%',
    trendClass: 'up'
  },
  {
    title: '点击量',
    value: '9520',
    icon: 'Mouse',
    changeDesc: '较上周 -12%',
    trendClass: 'down'
  },
  {
    title: '新用户',
    value: '156',
    icon: 'DataLine',  // 使用DataLine代替TrendChart
    changeDesc: '较上周 +30%',
    trendClass: 'up'
  }
]);

// 用户概览数据
const userGrowth = 23;
const userStats = ref([
  { label: '总用户量', value: '32k' },
  { label: '总访问量', value: '128k' },
  { label: '日访问量', value: '1.2k' },
  { label: '周同比', value: '+5%' }
]);

// 访问量数据
const visitGrowth = 15;
const lineChartData = ref([50, 25, 40, 20, 70, 35, 65, 30, 35, 20, 45, 45]);
const months = ref(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']);

// ECharts 实例
const barChart = ref(null);
const lineChart = ref(null);

// 计算属性：用户增长样式
const userGrowthClass = computed(() => (userGrowth > 0 ? 'text-success' : 'text-danger'));
// 计算属性：访问量增长样式
const visitGrowthClass = computed(() => (visitGrowth > 0 ? 'success-tag' : 'danger-tag'));

onMounted(() => {
  initBarChart();
  initLineChart();
});

/**
 * 初始化柱状图（用户行为）
 */
const initBarChart = () => {
  const myChart = echarts.init(barChart.value);
  myChart.setOption({
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: [1, 2, 3, 4, 5, 6, 7, 8, 9],
      axisLabel: { color: '#666' },
      axisLine: { lineStyle: { color: '#eee' } }
    },
    yAxis: {
      type: 'value',
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#f5f7fa' } }
    },
    series: [
      {
        name: '用户行为',
        type: 'bar',
        data: [150, 100, 150, 80, 190, 100, 180, 120, 160],
        itemStyle: { color: '#66b1ff' },
        barWidth: '35%',
        emphasis: { disabled: true } 
      }
    ],
    grid: { left: '8%', right: '8%', bottom: '10%', containLabel: true }
  });
};

/**
 * 初始化折线图（访问量趋势）
 */
const initLineChart = () => {
  const myChart = echarts.init(lineChart.value);
  myChart.setOption({
    tooltip: { 
      trigger: 'axis', 
      formatter: '{b} <br/>{c} 次' 
    },
    xAxis: {
      type: 'category',
      data: months.value,
      axisLabel: { color: '#666' },
      axisLine: { lineStyle: { color: '#eee' } }
    },
    yAxis: {
      type: 'value',
      axisLabel: { color: '#999' },
      splitLine: { lineStyle: { color: '#f5f7fa' } }
    },
    series: [
      {
        name: '访问量',
        type: 'line',
        data: lineChartData.value,
        lineStyle: { color: '#66b1ff' },
        symbol: 'circle',
        symbolSize: 6,
        smooth: true, 
        areaStyle: { 
          color: new echarts.graphic.LinearGradient(
            0, 0, 0, 1, 
            [{ offset: 0, color: 'rgba(102, 177, 255, 0.2)' }, 
             { offset: 1, color: 'rgba(102, 177, 255, 0)' }]
          ) 
        },
        emphasis: { 
          disabled: true,
          lineStyle: { color: '#409eff' },
          areaStyle: { color: 'rgba(102, 177, 255, 0.3)' }
        }
      }
    ],
    grid: { left: '8%', right: '8%', bottom: '10%', containLabel: true }
  });

  // 监听窗口变化
  window.addEventListener('resize', () => myChart.resize());
};
</script>

<style scoped>
.dashboard {
  margin: -20px;
  padding: 24px;
  background-color: #f9fafb;
  min-height: 100vh;
  box-sizing: border-box;
}

/* 顶部统计卡片 */
.stats-header {
  margin-bottom: 24px;
}

.stat-card {
  background: #fff;
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.05);
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

.stat-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-title {
  font-size: 14px;
  color: #909399;
  margin: 0 0 8px;
}

.stat-value-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-value {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.info-icon {
  color: #909399;
  cursor: pointer;
}

.stat-change {
  font-size: 12px;
  margin: 4px 0 0;
}

.stat-change.up { color: #67c23a; }
.stat-change.down { color: #f56c6c; }

.stat-icon {
  min-width: 40px;
  display: flex;
  justify-content: center;
  color: #66b1ff;
}

/* 核心内容区域 */
.main-content {
  display: flex;
  flex-wrap: wrap;
}

/* 用户分析卡片 */
.user-analysis-card, .visit-analysis-card {
  background: #fff;
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.05);
  margin-bottom: 24px;
  display: flex;
  flex-direction: column;
}

.chart-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f5f7fa;
}

.chart-title h3 {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.growth-tag {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.success-tag {
  background: #f0f9eb;
  color: #67c23a;
}

.danger-tag {
  background: #fef0f0;
  color: #f56c6c;
}

/* 图表样式 */
.bar-chart, .line-chart {
  flex: 1;
  margin: 20px;
}

/* 用户概述 */
.user-overview {
  padding: 20px;
}

.user-overview h4 {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 12px;
}

.growth-info {
  font-size: 12px;
  color: #606266;
  margin: 0 0 8px;
}

.desc {
  font-size: 12px;
  color: #909399;
  line-height: 1.6;
  margin: 0 0 16px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.stat-item {
  text-align: center;
}

.item-value {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  display: block;
  margin-bottom: 4px;
}

.item-label {
  font-size: 12px;
  color: #999;
}

/* 响应式适配 */
@media (max-width: 1200px) {
  .user-analysis-card, .visit-analysis-card {
    --el-col-span: 24;
  }
}

@media (max-width: 768px) {
  .stat-content {
    flex-direction: column;
    text-align: center;
  }
  .stat-icon {
    margin-top: 12px;
  }
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
</style>